package com.easemob.chatui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMMessage;
import com.easemob.chat.TextMessageBody;
import com.easemob.chatui.Constant;
import com.google.gson.Gson;
import com.lyw.GeoLocationClient.App;
import com.lyw.GeoLocationClient.R;
import com.lyw.GeoLocationClient.bean.BmobUser;
import com.lyw.GeoLocationClient.json.Task;
import com.lyw.GeoLocationClient.service.LBSClient;
import timber.log.Timber;

import java.util.List;

public class TaskInfoActivity extends BaseActivity {

    private static final String USER = "user";
    private static final String INDEX = "index";
    private BmobUser mUser;
    private EditText mTitle;
    private EditText mIntro;

    Task mTask;
    int index;


    public static void starTaskInfoActivityActivityForResult(Activity context, BmobUser user, int task_index) {
        Intent intent = new Intent();
        intent.putExtra(USER, user);
        intent.putExtra(INDEX, task_index);
        intent.setClass(context, TaskInfoActivity.class);
        context.startActivityForResult(intent, 0);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_info);

        mUser = (BmobUser) getIntent().getSerializableExtra(USER);
        Timber.d(mUser.toString());
        index = getIntent().getIntExtra(INDEX, 0);
        String task = mUser.getTasks().get(index);
        Gson gson = new Gson();
        mTask = gson.fromJson(task, Task.class);

        mTitle = (EditText) findViewById(R.id.edit_title);
        mIntro = (EditText) findViewById(R.id.edit_introduction);

        mTitle.setText(mTask.getTitle());
        mIntro.setText(mTask.getTask());

        if (App.isAdmin() || mTask.isDone()) {
            findViewById(R.id.submit).setVisibility(View.GONE);
        }
    }

    /**
     * @param v
     */
    public void submit(View v) {
        mTask.setIsDone(true);
        String task = new Gson().toJson(mTask);

        mUser.getTasks().set(index, task);

        mUser.update(this);

        getIntent().putExtra(USER, mUser);
        setResult(RESULT_OK, getIntent());

        sendCompleteTaskMessage(task);

        finish();
    }

    private void sendCompleteTaskMessage(String taskSt) {
        EMMessage message = null;
        TextMessageBody txtBody = null;

        message = EMMessage.createSendMessage(EMMessage.Type.TXT);
        txtBody = new TextMessageBody(taskSt);
        message.setAttribute(Constant.MESSAGE_COMPLETE_TASK, true);

        message.addBody(txtBody);


        List<BmobUser> adminList = LBSClient.getInstance().getmAdminList();

        for (BmobUser user : adminList) {
            message.setReceipt(user.getUsername());

            EMChatManager.getInstance().sendMessage(message, new EMCallBack() {
                @Override
                public void onSuccess() {
                    Timber.d("success");
                }

                @Override
                public void onError(int i, String s) {
                    Timber.d("onError " + s);

                }

                @Override
                public void onProgress(int i, String s) {
                    Timber.d("onError " + i + " " + s);

                }
            });

        }
    }
}
